import { BuildingConfig } from './config/MapConfig';

export class Building extends Laya.Image {
    public type: string;
    public gridX: number = 0;
    public gridY: number = 0;
    public gridWidth: number;
    public gridHeight: number;
    id:number=0

    constructor() {
        super();
        this.type = '';
        this.gridWidth = 1;
        this.gridHeight = 1;
    }

    public initBuilding(config: BuildingConfig, gridSize: number) {
        this.type = config.type;
        this.gridX = config.position.x;
        this.gridY = config.position.y;
        this.gridWidth = Math.ceil(config.size.width / gridSize);
        this.gridHeight = Math.ceil(config.size.height / gridSize);
        
        // 设置建筑皮肤
        this.skin = this.type + '.png';
        
        // 设置建筑大小
        this.size(this.gridWidth * gridSize, this.gridHeight * gridSize);
        
        // 设置建筑位置
        this.pos(this.gridX * gridSize, this.gridY * gridSize);
    }

    public getConfig(): BuildingConfig {
        return {
            id:this.id,
            type: this.type,
            position: {
                x: this.gridX,
                y: this.gridY
            },
            size: {
                width: this.width,
                height: this.height
            }
        };
    }
}